package net.bwie.realtime.dws.log.job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.bwie.realtime.userProfile.utils.CDCUtil;
import net.bwie.realtime.userProfile.utils.DorisUtil;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class FlinkToKafka {

    public static void main(String[] args) throws  Exception{

        //1.执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        env.enableCheckpointing(3000L);

        //2.读取数据源
        DataStreamSource<String> weatherStream = CDCUtil.getMysqlSource(env, "jtp_data_warehouse");
//        weatherStream.print();

        //3.数据处理
        DataStream<String> resultStream = processWeather(weatherStream);
        resultStream.print();
        //4. 数据输出
//        KafkaUtil.producerKafka(resultStream, "dwd_weather_info");

        DorisUtil.saveToDoris(resultStream, "user_realtime_report", "ods_weather_info");
        //5. 执行
        env.execute("FlinkToKafka");
    }

    private static DataStream<String> processWeather(DataStream<String> stream) {

        SingleOutputStreamOperator<String> mapStream = stream.map(new MapFunction<String, String>() {
            @Override
            public String map(String value) throws Exception {
                JSONObject jsonObject = JSON.parseObject(value);
                String after = jsonObject.getJSONObject("after").toJSONString();
                return after;
            }
        });
        return mapStream;
    }
}
